A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

Regular Procedure Bound To Class Method


See also:
program testing123;
{$MODE OBJFPC} {$H+} {$M+}

type
  TTest = class
    procedure test(i: integer);
  end;

var Tester: TTest;

procedure TTest.test(i: integer);
begin
  writeln(i, ' ', i, ' ', i);
end;

function GetMethodAddress: pointer;
begin
  result:= tester.MethodAddress('test');;
end;

function GetClassPointer: pointer;
begin
  result:= pointer(tester);
end;

// NO OBJECT OR CLASS REQUIRED TO LAUNCH PROCEDURE
var test: procedure (self: pointer; i: integer);
var fakeself: pointer;

procedure FuckClasses;
begin
  fakeself:= GetClassPointer;
  pointer(test):= GetMethodAddress;
end;

begin

  Tester:= TTest.create;

  // bind the procedure to a class method and get the class instance
  FuckClasses; 

  // now we can launch the class methods without any class
  test(fakeself, 888);  
  // above is the same as doing:
  // Tester.test(888);  
  // it's only syntax sugar differences

  Tester.free;
end.

Why would this be useful? DLL, plugin. Make use of existing class code inside existing software... export all the class methods.. then have plugin writers using C/C++/FPC/Delphi talk to your software without any package system required (BPL).

Advantages are that you don't use a proprietary package system that may or may not be cross compiler compatible (fpc/delphi/c++/c).

My other attempts at exporting classes have included using virtual classes and then inheriting them in the DLL - but this isn't cross compiler compabitle if some asshole has a Cee compiler and wants to build a plugin for your app (or NameYourLanguageHere).

You could build a procedure to query whether or not the class instance is alive. In the above code I create and free the class.. but in a DLL you'd have to make sure the class was available to use.

How would you export your existing class infrastructure inside your executable (an existing program that has no plugin system)? Well, you would export functions from executable and have the DLL import them from the EXE. Yes, you can export from the EXE, not just the DLL. Unfortunately, many software programmers don't realize you can export from the EXE.

Export the GetMethodAddress and GetClassPointer procedures from the EXE and put the FuckClasses procedure in your DLL and call the GetMethodAddress and GetClassPointer routines from within you DLL after you've imported them from the EXE. Then you can use the methods from your classes by simply setting up regular old procedures in your DLL to bind to the method addresses and class instance pointers.

I'm not sure what class functionality won't work.. for example I haven't tested exporting abstract/virtual/overriden/overloaded methods and other fucked up class nonsense. Well actually I tested overloaded functions and found that the MethodAddress returns the first procedure in the class.

{$MODE OBJFPC} {$H+} {$M+}

type
  TTest = class
    procedure test(s: string);  // MethodAddress will return this one
    procedure test(i: integer);
  end;
Not sure how to gain access to the second overloaded one.

About
This site is about programming and other things.
_ _ _